package com.konsung.ui.defineview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.konsung.healthfile.R;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 圆形进度提示
 */
public class WaitingDialog extends Dialog {
    private Context context; //上下文
    private String title = ""; //标题
    private UpdataButtonState updataButtonState; //监听对象
    /**
     * 是否数据库升级的弹窗
     */
    private boolean isDbUpdate = false;

    @BindView(R.id.msg)
    TextView tips;

    /**
     * 取消监听
     */
    public interface UpdataButtonState {
        /**
         * 获取按钮状态
         *
         * @param pressed 状态值
         */
        public void getButton(Boolean pressed);
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param title   进度标题
     */
    public WaitingDialog(Context context, String title) {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_waiting);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.dg_content);
        linearLayout.getBackground().setAlpha(90);
        ButterKnife.bind(this);
    }

    @Override
    public void show() {
        super.show();
       /* // set dialog enter animations
        contentView.startAnimation(AnimationUtils.loadAnimation(context, R
                .anim.dialog_main_show_amination));*/
    }

    @Override
    public void dismiss() {
       /* Animation anim = AnimationUtils.loadAnimation(context, R.anim
                .dialog_main_hide_amination);
        anim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                contentView.post(new Runnable() {
                    @Override
                    public void run() {
                        WaitingDialog.super.dismiss();
                    }
                });

            }
        });

        Animation backAnim = AnimationUtils.loadAnimation(context, R.anim
                .dialog_root_hide_amin);
        contentView.startAnimation(anim);*/
        super.dismiss();
    }

    /**
     * 是否数据库升级的弹窗
     *
     * @param dbUpdate dbUpdate
     */
    public void setDbUpdate(boolean dbUpdate) {
        isDbUpdate = dbUpdate;
    }

    /**
     * 设置提示框标题
     *
     * @param title 标题字符
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 设置提示框内容文本
     *
     * @param tips 内容字符
     */
    public void setText(String tips) {
        this.tips.setText(tips);
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {

        if (isDbUpdate) {
            if (KeyEvent.KEYCODE_BACK == keyCode) {
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
